package common;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.DocumentHelper;

public class XmlParseUtil
{
    @SuppressWarnings("rawtypes")
    public static Map<String, Object> parseXml2Map(String xml,
                    Class<?> cls) throws Exception
    {
        Map<String, Object> maps = new HashMap<String, Object>();
        List<Object> lists = new ArrayList<Object>();
        Document doc = DocumentHelper.parseText(xml);
        Element et = doc.getRootElement();

        // set lists is empty.
        // boolean bool = true ;
        // The name of the first node.
        List elements = et.elements();
        @SuppressWarnings("unchecked")
        List<Element> rList = elements;
        if (rList.isEmpty())
        {
            if (null != doc.getStringValue() && !doc.getStringValue().isEmpty())
            {
                maps.put(et.getName(), doc.getStringValue());
            }
        }
        else
        {
            for (Element element : rList)
            {
                @SuppressWarnings("unchecked")
                List<Element> rLists = element.elements();
                if (!rLists.isEmpty() && rLists.size() > 0)
                {
                    // bool = false;
                    // Verify second node
                    for (Element e : rLists)
                    {
                        @SuppressWarnings("unchecked")
                        List<Element> li = e.elements();
                        Class<?> cl = (Class<?>) Class.forName(cls.getName());
                        Object ob = cl.newInstance();
                        for (Element element2 : li)
                        {
                            String name = element2.getName();
                            String value = element2.getText();
                            Field field = ob.getClass().getDeclaredField(name);
                            field.setAccessible(true);
                            convertValue(field, ob, value);
                        }
                        lists.add(ob);
                    }
                }
                else
                {
                    maps.put(element.getName(), element.getText());
                }
                maps.put(cls.getSimpleName() + "_List", lists);
            }
        }

        return maps;
    }

    public static void convertValue(Field field, Object obj, String value)
                    throws Exception
    {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (field.getGenericType().toString().equals("class java.lang.Integer"))
        {
            field.set(obj, Integer.parseInt(value));
        }
        else if (field.getGenericType().toString().equals("boolean"))
        {
            field.set(obj, Boolean.parseBoolean(value));
        }
        else if (field.getGenericType().toString().equals(
            "class java.util.Date"))
        {
            field.set(obj, sim.parse(value));
        }
        else
        {
            field.set(obj, value);
        }

    }
}
